package com.feihao.zhou.demo01;

import java.sql.*;

/**
 * 查看db3的user表
 */
public class JDBCDemo01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.注册驱动：
        Class.forName("com.mysql.jdbc.Driver");   //5.5把呢不能后可省略

        //2.获取连接

        //url:jdbc:mysql://ip地址（域名）:端口号/数据库名称
        Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.26.1:3306/db3", "root", "root");
        //3.获取执行者对象
        //获取普通执行者对象
        Statement statement = connection.createStatement();

        //
        //4.执行sql语句

        //DQL语句：
        ResultSet resultSet = statement.executeQuery("SELECT * FROM USER ");
        //5.处理结果

        while (resultSet.next()) {
            System.out.println(resultSet.getInt("id") + "\t" + resultSet.getString("name"));
        }


        int i = statement.executeUpdate("INSERT INTO USER VALUES (5,'田七','湖南');");
        System.out.println(i);

        System.out.println("================增加后在查询-=======");

        //DQL语句：
        ResultSet resultSet1 = statement.executeQuery("SELECT * FROM USER ");
        //5.处理结果

        while (resultSet1.next()) {
            System.out.println(resultSet1.getInt("id") + "\t" + resultSet1.getString("name"));
        }

        //6.释放资源：

        connection.close();
        statement.close();
        resultSet.close();
    }
}
